/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZSearchParams;
/*     */ import com.zimbra.client.ZSearchParams.Cursor;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.soap.type.SearchSortBy;
/*     */ import java.nio.file.FileSystem;
/*     */ import java.nio.file.FileSystems;
/*     */ import java.nio.file.Files;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestBug89152
/*     */   extends TestCase
/*     */ {
/*     */   private static final String RECIPIENT1 = "search_test";
/*     */   private static final String PASSWORD = "test123";
/*     */   private static final String SEARCH_STRING = "堀江";
/*     */   private static final String TEST_MESSAGE_FILE = "/Users/gsolovyev/p4/main/ZimbraQA/data/TestMailRaw/bug89152/testmessage.eml";
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  53 */     cleanUp();
/*  54 */     Map<String, Object> attrs = new HashMap();
/*  55 */     attrs.put("zimbraMailHost", LC.zimbra_server_hostname.value());
/*  56 */     attrs.put("zimbraAttachmentsIndexingEnabled", Boolean.valueOf(true));
/*  57 */     attrs.put("cn", "search_test1");
/*  58 */     attrs.put("displayName", "TestAccount unit test user 1");
/*  59 */     Provisioning.getInstance().createAccount(TestUtil.getAddress("search_test"), "test123", attrs);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessagesWithDifferentTimestamps()
/*     */   {
/*     */     try {
/*  66 */       int numMessages = 2207;
/*  67 */       int firstLimit = 100;
/*  68 */       int incLimit = 50;
/*  69 */       int offset = 0;
/*  70 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test");
/*  71 */       long timestamp = System.currentTimeMillis() - 216000000L;
/*  72 */       byte[] msgBytes = Files.readAllBytes(FileSystems.getDefault().getPath("/Users/gsolovyev/p4/main/ZimbraQA/data/TestMailRaw/bug89152/testmessage.eml", new String[0]));
/*  73 */       for (int i = 0; i < numMessages; i++) {
/*  74 */         ParsedMessage pm = new ParsedMessage(msgBytes, Long.valueOf(timestamp), true);
/*  75 */         Message msg = TestUtil.addMessage(recieverMbox, pm);
/*  76 */         timestamp += 1001L;
/*     */       }
/*     */       
/*  79 */       Thread.sleep(150L);
/*  80 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test");
/*  81 */       ZSearchParams searchParams = new ZSearchParams("in:inbox 堀江");
/*  82 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/*  83 */       searchParams.setLimit(firstLimit);
/*  84 */       searchParams.setTypes("message");
/*  85 */       List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
/*  86 */       assertEquals(firstLimit, resultSet.size());
/*  87 */       int gotMessages = resultSet.size();
/*  88 */       List<String> seenIds = new ArrayList();
/*  89 */       ZMessage lastHit = null;
/*  90 */       for (ZMessage m : resultSet) {
/*  91 */         seenIds.add(m.getId());
/*  92 */         lastHit = m;
/*     */       }
/*  94 */       int recCount = 1;
/*  95 */       offset += firstLimit;
/*  96 */       while (gotMessages > 0) {
/*  97 */         searchParams = new ZSearchParams("in:inbox 堀江");
/*  98 */         searchParams.setCursor(new ZSearchParams.Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate())));
/*  99 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 100 */         searchParams.setLimit(incLimit);
/* 101 */         searchParams.setTypes("message");
/* 102 */         searchParams.setOffset(offset);
/* 103 */         resultSet = TestUtil.search(zmbx, searchParams);
/* 104 */         recCount++;
/* 105 */         gotMessages = resultSet.size();
/* 106 */         int resCount = 0;
/* 107 */         for (ZMessage m : resultSet) {
/* 108 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), m.getId(), Integer.valueOf(seenIds.indexOf(m.getId())) }), seenIds.contains(m.getId()));
/* 109 */           seenIds.add(m.getId());
/* 110 */           resCount++;
/* 111 */           lastHit = m;
/*     */         }
/* 113 */         Thread.sleep(300L);
/* 114 */         offset += incLimit;
/*     */       }
/* 116 */       assertEquals("expecting " + numMessages + " messages. Got " + seenIds.size(), numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 118 */       e.printStackTrace();
/* 119 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 121 */       e.printStackTrace();
/* 122 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessagesWithSimilarTimestamps() {
/*     */     try {
/* 129 */       int numMessages = 5207;
/* 130 */       int firstLimit = 150;
/* 131 */       int incLimit = 50;
/* 132 */       int offset = 0;
/* 133 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test");
/* 134 */       long timestamp = System.currentTimeMillis();
/* 135 */       byte[] msgBytes = Files.readAllBytes(FileSystems.getDefault().getPath("/Users/gsolovyev/p4/main/ZimbraQA/data/TestMailRaw/bug89152/testmessage.eml", new String[0]));
/* 136 */       for (int i = 0; i < numMessages; i++) {
/* 137 */         ParsedMessage pm = new ParsedMessage(msgBytes, Long.valueOf(timestamp), true);
/* 138 */         pm.getMimeMessage().setSentDate(new Date());
/* 139 */         Message msg = TestUtil.addMessage(recieverMbox, pm);
/* 140 */         timestamp += 1L;
/*     */       }
/*     */       
/* 143 */       Thread.sleep(150L);
/* 144 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test");
/* 145 */       ZSearchParams searchParams = new ZSearchParams("in:inbox 堀江");
/* 146 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 147 */       searchParams.setLimit(firstLimit);
/* 148 */       searchParams.setTypes("message");
/* 149 */       List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
/* 150 */       assertEquals(firstLimit, resultSet.size());
/* 151 */       int gotMessages = resultSet.size();
/* 152 */       List<String> seenIds = new ArrayList();
/* 153 */       ZMessage lastHit = null;
/* 154 */       for (ZMessage m : resultSet) {
/* 155 */         seenIds.add(m.getId());
/* 156 */         lastHit = m;
/*     */       }
/* 158 */       int recCount = 1;
/* 159 */       offset += firstLimit;
/* 160 */       while (gotMessages > 0) {
/* 161 */         searchParams = new ZSearchParams("in:inbox 堀江");
/* 162 */         searchParams.setCursor(new ZSearchParams.Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate())));
/* 163 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 164 */         searchParams.setLimit(incLimit);
/* 165 */         searchParams.setTypes("message");
/* 166 */         searchParams.setOffset(offset);
/* 167 */         resultSet = TestUtil.search(zmbx, searchParams);
/* 168 */         recCount++;
/* 169 */         gotMessages = resultSet.size();
/* 170 */         int resCount = 0;
/* 171 */         for (ZMessage m : resultSet) {
/* 172 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), m.getId(), Integer.valueOf(seenIds.indexOf(m.getId())) }), seenIds.contains(m.getId()));
/* 173 */           seenIds.add(m.getId());
/* 174 */           resCount++;
/* 175 */           lastHit = m;
/*     */         }
/* 177 */         Thread.sleep(300L);
/* 178 */         offset += incLimit;
/*     */       }
/* 180 */       assertEquals("expecting " + numMessages + " messages. Got " + seenIds.size(), numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 182 */       e.printStackTrace();
/* 183 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 185 */       e.printStackTrace();
/* 186 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 195 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 199 */     Provisioning prov = Provisioning.getInstance();
/* 200 */     Account account = prov.get(Key.AccountBy.name, TestUtil.getAddress("search_test"));
/* 201 */     if (account != null) {
/* 202 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestBug89152.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */